﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Input;
using VehicleRental.WpfClient.Behavior;

namespace VehicleRental.WpfClient.ViewModels
{
    public class PlacesViewModel : ViewModelBase
    {
        public string Title { get; set; }

        public decimal Longitude { get; set; }

        public decimal Latitude { get; set; }

        public string Address { get; set; }

        public string Description { get; set; }

        public string ImageSource { get; set; }

        private ICommand prevVehicle;
        private ICommand nextVehicle;

        private VehicleViewModel currentVehicle;
        private ObservableCollection<VehicleViewModel> vehicles;

        public IEnumerable<VehicleViewModel> VehiclesEnum { get; set; }

        public ICommand PrevVehicle
        {
            get
            {
                if (this.prevVehicle == null)
                {
                    this.prevVehicle = new RelayCommand(this.HandlePreviousCommand);
                }
                return this.prevVehicle;
            }
        }

        public ICommand NextVehicle
        {
            get
            {
                if (this.nextVehicle == null)
                {
                    this.nextVehicle = new RelayCommand(this.HandleNextCommand);
                }
                return this.nextVehicle;
            }
        }

        public VehicleViewModel CurrentVehicle
        {
            get
            {
                if (this.currentVehicle == null)
                {
                    var imagesCollectionView = CollectionViewSource.GetDefaultView(this.VehiclesEnum);
                    this.currentVehicle = imagesCollectionView.CurrentItem as VehicleViewModel;
                }
                return this.currentVehicle;
            }

            set
            {
                this.currentVehicle = value;
                this.OnPropertyChanged("CurrentVehicle");
            }
        }

        public ObservableCollection<VehicleViewModel> Vehicles
        {
            get
            {
                if (this.vehicles == null)
                {
                    this.vehicles = new ObservableCollection<VehicleViewModel>();
                    foreach (var vehicle in VehiclesEnum)
                    {
                        this.vehicles.Add(vehicle);
                    }
                }

                return vehicles;
            }
        }

        private void HandlePreviousCommand(object obj)
        {
            var vehiclesCollectionView = CollectionViewSource.GetDefaultView(this.Vehicles);
            vehiclesCollectionView.MoveCurrentToPrevious();
            if (vehiclesCollectionView.IsCurrentBeforeFirst)
            {
                vehiclesCollectionView.MoveCurrentToLast();
            }

            var current = vehiclesCollectionView.CurrentItem as VehicleViewModel;
            this.CurrentVehicle = current;
        }

        private void HandleNextCommand(object obj)
        {
            var vehiclesCollectionView = CollectionViewSource.GetDefaultView(this.Vehicles);
            vehiclesCollectionView.MoveCurrentToNext();
            if (vehiclesCollectionView.IsCurrentAfterLast)
            {
                vehiclesCollectionView.MoveCurrentToFirst();
            }

            var current = vehiclesCollectionView.CurrentItem as VehicleViewModel;
            this.CurrentVehicle = current;
        }
    }
}
